package chpt09;

import java.util.Random;

public class JoinTest {
	public static void main(String[] args) {
		System.out.println("-----比赛开始-----");
		joinTest();
		System.out.println("-----比赛结束-----");
	}
	private static void joinTest() {
		Thread t1 = new Thread(new Runner());
		Thread t2 = new Thread(new Runner());
		Thread t3 = new Thread(new Runner());
		t1.start();	t2.start();	t3.start();
		try {
			t1.join();t2.join();t3.join();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
}


class Runner implements Runnable {

	@Override
	public void run() {
		Random rnd = new Random();
		String name = Thread.currentThread().getName();
		for (int i = 0; i < 3; i++) {
			try {
				Thread.sleep(rnd.nextInt(1000));
				System.out.println(name + " running");
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		System.out.println(name + " 完成.");
	}
}
